<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 清空权限表
        Permission::query()->truncate();

        // 创建仪表盘
        $this->createDashboard();
        // 创建内容
        $this->createContent();
//        // 商户用户
//        $this->createMerchantUser();
//        // 创建商品
//        $this->createProduct();
//        // 创建订单
//        $this->createOrder();

        // 创建系统设置
        $this->createSetting();
        // 技术
        $this->createTechnical();

        $permissions = Permission::query()->get(['id'])->pluck('id')->toArray();
        $role = Role::query()->where('slug', 'admin')->first();
        $role->permissions()->sync($permissions);
    }

    /**
     * 创建仪表盘
     */
    private function createDashboard(): void
    {
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '主页',
            'icon' => 'icon-home',
            'name' => 'admin.dashboard',
            'active' => 'admin.dashboard',
            'redirect' => 'admin.dashboard',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);
    }

    /**
     * 创建商户用户
     */
    private function createMerchantUser(): void
    {
        $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '商户用户',
            'icon' => 'icon-user',
            'name' => 'admin.merchant_user',
            'active' => 'admin.merchant_user',
            'redirect' => 'admin.merchant_user.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 25,
        ]);
    }

    /**
     * 创建CMS
     */
    private function createContent(): void
    {
        $cms = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '内容',
            'icon' => 'icon-topic',
            'name' => 'admin.content',
            'active' => 'admin.content',
            'redirect' => 'admin.content.articles.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->create([
            'parent_id' => $cms->id,
            'type' => Permission::TYPE_MENU,
            'title' => '文章管理',
            'icon' => '',
            'name' => 'admin.content.articles.index',
            'active' => 'admin.content',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $this->create([
            'parent_id' => $cms->id,
            'type' => Permission::TYPE_MENU,
            'title' => '分类管理',
            'icon' => '',
            'name' => 'admin.content.article.categories.index',
            'active' => 'admin.content',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->create([
            'parent_id' => $cms->id,
            'type' => Permission::TYPE_MENU,
            'title' => '标签管理',
            'icon' => '',
            'name' => 'admin.content.article.tags.index',
            'active' => 'admin.content',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);

//        $this->create([
//            'parent_id' => $cms->id,
//            'type' => Permission::TYPE_MENU,
//            'title' => '评论管理',
//            'icon' => '',
//            'name' => 'admin.content.article.comments.index',
//            'active' => 'admin.content',
//            'redirect' => '',
//            'status' => 1,
//            'is_show' => true,
//            'sort' => 40,
//        ]);

        $this->create([
            'parent_id' => $cms->id,
            'type' => Permission::TYPE_MENU,
            'title' => '友情链接',
            'icon' => '',
            'name' => 'admin.content.article.links.index',
            'active' => 'admin.content',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 50,
        ]);


    }

    /**
     * 创建商品
     */
    private function createProduct(): void
    {
        $product = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '商品',
            'icon' => 'icon-product',
            'name' => 'admin.product',
            'active' => 'admin.product',
            'redirect' => 'admin.product.product.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);

        $this->create([
            'parent_id' => $product->id,
            'type' => Permission::TYPE_MENU,
            'title' => '商品列表',
            'icon' => '',
            'name' => 'admin.product.product.index',
            'active' => 'admin.product',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $this->create([
            'parent_id' => $product->id,
            'type' => Permission::TYPE_MENU,
            'title' => '商品分类',
            'icon' => '',
            'name' => 'admin.product.category.index',
            'active' => 'admin.product',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

    }

    public function createOrder()
    {
        $order = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '订单',
            'icon' => 'icon-order',
            'name' => 'admin.order',
            'active' => 'admin.order',
            'redirect' => 'admin.order.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 40,
        ]);

        $this->create([
            'parent_id' => $order->id,
            'type' => Permission::TYPE_MENU,
            'title' => '订单列表',
            'icon' => '',
            'name' => 'admin.order.index',
            'active' => 'admin.order',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        // 退款订单
        $this->create([
            'parent_id' => $order->id,
            'type' => Permission::TYPE_MENU,
            'title' => '退款订单',
            'icon' => '',
            'name' => 'admin.order.refund.index',
            'active' => 'admin.order',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        // 投诉订单
        $this->create([
            'parent_id' => $order->id,
            'type' => Permission::TYPE_MENU,
            'title' => '投诉订单',
            'icon' => '',
            'name' => 'admin.order.complaint.index',
            'active' => 'admin.order',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);
    }

    /**
     * 创建系统设置
     */
    private function createSetting(): void
    {
        $setting = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '设置',
            'icon' => 'icon-settings',
            'name' => 'admin.setting',
            'active' => 'admin.setting',
            'redirect' => 'admin.setting.website.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 90,
        ]);

        $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '站点设置',
            'icon' => 'icon-config',
            'name' => 'admin.setting.website.index',
            'active' => 'admin.setting',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '用户管理',
            'icon' => 'icon-user',
            'name' => 'admin.setting.users.index',
            'active' => 'admin.setting',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '角色管理',
            'icon' => 'icon-developer',
            'name' => 'admin.setting.roles.index',
            'active' => 'admin.setting',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->create([
            'parent_id' => $setting->id,
            'type' => Permission::TYPE_MENU,
            'title' => '权限管理',
            'icon' => 'icon-duplicate',
            'name' => 'admin.setting.permissions.index',
            'active' => 'admin.setting',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);



    }

    /**
     * 创建技术
     */
    private function createTechnical(): void
    {
        $technical = $this->create([
            'parent_id' => 0,
            'type' => Permission::TYPE_MENU,
            'title' => '技术',
            'icon' => 'icon-technology',
            'name' => 'admin.technical',
            'active' => 'admin.technical',
            'redirect' => 'admin.technical.logs.index',
            'status' => 1,
            'is_show' => true,
            'sort' => 100,
        ]);

        $this->create([
            'parent_id' => $technical->id,
            'type' => Permission::TYPE_MENU,
            'title' => '操作日志',
            'icon' => '',
            'name' => 'admin.technical.logs.index',
            'active' => 'admin.technical',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 10,
        ]);

        $this->create([
            'parent_id' => $technical->id,
            'type' => Permission::TYPE_MENU,
            'title' => '登录日志',
            'icon' => '',
            'name' => 'admin.technical.logins.index',
            'active' => 'admin.technical',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 20,
        ]);

        $this->create([
            'parent_id' => $technical->id,
            'type' => Permission::TYPE_MENU,
            'title' => '访问日志',
            'icon' => '',
            'name' => 'admin.technical.access.index',
            'active' => 'admin.technical',
            'redirect' => '',
            'status' => 1,
            'is_show' => true,
            'sort' => 30,
        ]);
    }


    public function create(array $data): Permission
    {
        return Permission::query()->create($data);
    }
}
